#include "app_common.h"
#include "sys/time.h"
#include "stddef.h"
#include "stdlib.h"
#include "stdio.h"
#include "log/log.h"
#include "string.h"
long app_common_getCurrentTime(void)
{
    //定义时间结构体
    struct timeval tv;
    //获取当前时间
    gettimeofday(&tv, NULL);
    //
    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

char *app_common_chars2hexstr(char *chars, int len)
{
    //定义一个长度为len的char数组
    char *hexstr = (char *)malloc(len * 2 + 1);
    for (int i = 0; i < len; i++)
    {
        //将chars[i]转换成16进制数，并保存在hexstr[i*2]和hexstr[i*2+1]中
        sprintf(hexstr + i * 2, "%02x", chars[i]);
    }
    hexstr[len * 2] = '\0';
    return hexstr;
}

char *app_common_hexstr2chars(char *hexstr, int *chars_len)
{
    //定义一个char数组
    int hexstr_len = strlen(hexstr);

    *chars_len = hexstr_len/2;

    char *chars = malloc(*chars_len);

    for(int i=0; i<hexstr_len; i+=2) {
        sscanf(hexstr+i, "%2x", (unsigned int *)(chars+i/2));
    }

    return chars;

}
